package com.sgj.controller;

import com.sgj.entity.User;
import com.sgj.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.*;

/**
 * @author 沈国纪
 * @date 2021/12/16 20:35
 */
@RestController
@RequestMapping("/test")
public class TestController {

	@Autowired
	private ThreadPoolTaskExecutor threadPoolTaskExecutor;

	@Autowired
	private UserService userService;

	@GetMapping("/hello")
	public List<User> hello() {
		CompletableFuture<User> u1 = CompletableFuture.supplyAsync(() -> userService.findByName("sgj"), threadPoolTaskExecutor);

		CompletableFuture<User> u2 = CompletableFuture.supplyAsync(() -> userService.findByName("ccc"), threadPoolTaskExecutor);

		CompletableFuture.allOf(u1, u2).join();

		try {
			return Arrays.asList(u1.get(), u2.get());
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
		return null;
	}

	@GetMapping("/index")
	public String index() {
		return "hello index";
	}

	@GetMapping("/error")
	public String error() {
		return "error";
	}

	@GetMapping("/update")
	@Secured({"ROLE_sale", "ROLE_manager"})
	public String update() {
		return "hello update";
	}
}